﻿using SkiaSharp;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TorchSharp.Modules;

namespace Form_YOLO_NET8
{
    public partial class Form_DistanceMeasurementcs : Form
    {
        public Form_DistanceMeasurementcs()
        {
            InitializeComponent();
        }

        public Bitmap bitmapImg = null;
        public string ImgDir = null;

        private void Form_DistanceMeasurementcs_Load(object sender, EventArgs e)
        {

        }

        private void btn_CheckImg_Click(object sender, EventArgs e)
        {
            try
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Title = "选择文件";
                    ofd.Filter = "JPG|*.jpg*";
                    ofd.Multiselect = true;

                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        string ImgDir = ofd.FileName;
                        Debug.Print($"已经打开文件地址：{ofd.FileName}");

                        bitmapImg = new Bitmap(ImgDir);
                        this.imageShow.InPutBuffer(bitmapImg);


                        APoint_X.Maximum = bitmapImg.Width;
                        BPoint_X.Maximum = bitmapImg.Width;

                        APoint_Y.Maximum = bitmapImg.Height;
                        BPoint_Y.Maximum = bitmapImg.Height;


                    }
                }
            }
            catch (Exception ex) { MessageBox.Show($"图像打开失败{ex.Message}"); Debug.Print($"图像打开失败{ex.Message}"); }

        }






        public void DrawingIMG(Bitmap BitImg, int X1, int Y1, int X2, int Y2)
        {
            try
            {
                Bitmap bitmap = (Bitmap)BitImg.Clone();
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    Pen pen = new Pen(Color.Blue, 1);

                    g.DrawString("A", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Red), new Point(X1 - 10, Y1 - 20));
                    if (Y1 + 40 < bitmap.Height) { g.DrawLine(pen, new Point(X1, Y1), new Point(X1, Y1 + 40)); }
                    if (Y1 - 40 > bitmap.Height) { g.DrawLine(pen, new Point(X1, Y1), new Point(X1, Y1 - 40)); }

                    g.DrawString("B", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Red), new Point(X2 - 10, Y2 - 20));
                    if (Y2 + 40 < bitmap.Height) { g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 40)); }
                    if (Y2 - 40 > bitmap.Height) { g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 - 40)); }

                    g.Save();


                    this.imageShow.InPutBuffer(bitmap);
                    g.Dispose();
                    bitmap.Dispose();
                    GC.Collect();
                }
            }
            catch (Exception ex) { Debug.Print($"报错：{ex.Message}"); }




        }

        public void ProportionalConversion(Bitmap BitImg, int X1, int Y1, int X2, int Y2)
        {
            try
            {
                Bitmap bitmap = (Bitmap)BitImg.Clone();
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    Pen pen = new Pen(Color.Blue, 1);

                    g.DrawString("A", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Red), new Point(X1 - 10, Y1 - 20));
                    g.DrawLine(pen, new Point(X1, Y1), new Point(X1, Y1 + 40));
                    g.DrawLine(pen, new Point(X1, Y1), new Point(X1, Y1 - 40));

                    g.DrawString("B", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Red), new Point(X2 - 10, Y2 - 20));
                    g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 + 40));
                    g.DrawLine(pen, new Point(X2, Y2), new Point(X2, Y2 - 40));

                    // 执行浮点除法
                    double result = (double)tb_ActualWidth.IntValue / (double)(X2 - X1);
                    double roundedResult = Math.Round(result, 3);


                    g.DrawString($"比例为:{roundedResult}", new Font(FontFamily.GenericMonospace, 10), new SolidBrush(Color.Red), new Point(X1 - 10, Y1 - 20));
                    g.DrawLine(pen, new Point(X1, Y1), new Point(X2, Y1));
                    tb_ScaleParameters.Text = roundedResult.ToString();


                    g.Save();


                    this.imageShow.InPutBuffer(bitmap);
                    g.Dispose();
                    bitmap.Dispose();
                    GC.Collect();
                }
            }
            catch (Exception ex) { Debug.Print($"报错：{ex.Message}"); }
        }


        private void APointValueChanged(object sender, double value)
        {
            DrawingIMG(bitmapImg, (int)APoint_X.Value, (int)APoint_Y.Value, (int)BPoint_X.Value, (int)BPoint_Y.Value);

        }

        private void btn_ProportionalConversion_Click(object sender, EventArgs e)
        {
            ProportionalConversion(bitmapImg, (int)APoint_X.Value, (int)APoint_Y.Value, (int)BPoint_X.Value, (int)BPoint_Y.Value);
        }

    }
}
